﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.IO;
using DbSharp.GuiRunner.UI;
using DbSharp.Framework.TestEngine;
using DbSharp.Framework.ReleaseManage;
using DbSharp.GuiRunner.Properties;

using DbSharp.Utils;
using DbSharp.Utils.IO;


namespace DbSharp.GuiRunner
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ShowSplashForm();


            string testCaseFile;
            TestRunScope runScope;
            string resultFileName;
            bool needShowHelp;
            bool canParse = TryParseArguments(args, out testCaseFile, out runScope, out resultFileName, out needShowHelp);
            if ((false == canParse) || needShowHelp)
            {
                ShowUsageInfoDialog();
                Application.Exit();
            }
            else
            {
                var mainForm = new FormTestConsole();
                mainForm.StartupTestcaseFile = testCaseFile;
                mainForm.AutoRunScope = runScope;
                Application.Run(mainForm);
            }
        }

        private static void ShowSplashForm()
        {
        }
 

        static bool TryParseArguments(string[] args, out string testCaseFile, out TestRunScope runScope, out string resultFileName, out bool needShowHelp)
        {
            testCaseFile = string.Empty;
            resultFileName = string.Empty;
            runScope = TestRunScope.All;
            needShowHelp = false;
            bool result = true;

            #region Parse command line

            for (int i = 0; i < args.Length; i++)
            {
                if (args.Contains("/?") || args.Contains("/help")) 
                {
                    needShowHelp = true;
                    break;
                }

                string arg = args[i];
                if (i == 0)
                {
                    testCaseFile = arg;
                    continue;
                } 

                string runScopeMark = @"/runScope=";
                if (string.Equals(arg.Trim(), runScopeMark, StringComparison.OrdinalIgnoreCase))
                {
                    string runScopeString = arg.Trim();
                    int index = runScopeString.IndexOf(runScopeMark, StringComparison.OrdinalIgnoreCase) + runScopeMark.Length;
                    runScopeString = runScopeString.Substring(index);
                    string[] testRunScopeEnums = Enum.GetNames(typeof(TestRunScope));
                    foreach (string item in testRunScopeEnums)
                    {
                        if (string.Equals(item, runScopeString, StringComparison.OrdinalIgnoreCase))
                        {
                            try
                            {
                                runScope = (TestRunScope)Enum.Parse(typeof(TestRunScope), item);
                            }
                            catch
                            { result = false; }
                        }
                    }
                }

                //string resultFileMark = "@/resultFile=";
                //if (string.Equals(arg.Trim(), resultFileMark, StringComparison.OrdinalIgnoreCase))
                //{
                //    string resultFileString = arg.Trim();
                //    int index = resultFileString.IndexOf(resultFileMark) + resultFileMark.Length;
                //    resultFileString = resultFileString.Substring(index);
                //    resultFileName = resultFileString;
                //    break;
                //}
            }

            #endregion

            return result;
        }



        static void ShowUsageInfoDialog()
        {
            string fmt = @"
Description:
  Perform performance or unit test for database.
 
Usage:
{0} testCaseFile [options]

options: 
/runScope=<scopeType>   --Only run test cases with specified type, by default scopeType is All.(<scopeType>= All | PerfTest| UnitTest | None)
/help                   --Show this help message
";
            //resultFile=<file name> --Direct output result to file rather than to GUI.

            string info = string.Format(fmt, PathHelper.GetAppFileNameWithoutPath());
            MessageBox.Show(info, "Usage", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}
